import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import { Security } from './class/security';
import { Page } from 'src/app/classes/page';

@Component({
  selector: 'app-security',
  templateUrl: './security.component.html',
  styleUrls: ['./security.component.sass']
})
export class SecurityComponent implements OnInit {

  public security: Security = {
    content: '',
    isPublic: 0,
    dontReplayMe: 0
  };

  public data: Security[];

  submitting = false;

  user = {
    author: 'Han Solo',
    avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png'
  };

  /**
   * 点击发布
   */
  handleSubmit(): void {
    this.submitting = true;

    this.http.post(`/csapi/say`, this.security).subscribe(() => {
      this.submitting = false;

      this.data = [
        ...this.data,
        {
          ...this.security
        }
      ];

      this.security.content = '';
    }, (error) => {
      this.submitting = false;
    });
  }

  constructor(private http: HttpClient) { }

  ngOnInit() {
    this.security.content = '';

    this.load();
  }

  /**
   * 加载秘密
   */
  load() {
    this.http.get(`/csapi/all`).subscribe((page: Page) => {
      this.data = [
        ...page.content
      ];
    });
  }
}
